package com.simas.component;

//import com.simas.common.CustomResources;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
//import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Keypad;

public class TwoImageButton extends Field {
	private String strLabel;
	private Bitmap bitmapImage0;
	private Bitmap bitmapImage1;
	private int intUPosX;
	private int intUPosY;
	
	public TwoImageButton(String strLabel, Bitmap bitmapImage0, Bitmap bitmapImage1, int intUPosX, int intUPosY) {				
		//super(style);
		
		this.strLabel = strLabel;
		this.bitmapImage0 = bitmapImage0;
		this.bitmapImage1 = bitmapImage1;
		
		int intDispWidth = Display.getWidth();
		double dblRatio = 1;
		if (intDispWidth < 480) {
			dblRatio = intDispWidth / 480d;
		}
		
		this.intUPosX = (int) (intUPosX * dblRatio);
		this.intUPosY = (int) (intUPosY * dblRatio);
	}	
	
	public int getPreferredHeight() {
		int intHeight = 0;
		if (bitmapImage0.getHeight() > bitmapImage1.getHeight()) {
			if (bitmapImage0.getHeight() > getFont().getHeight()) {
				intHeight = bitmapImage0.getHeight() + 8;
			} else {
				intHeight = getFont().getHeight() + 8;
			}
		} else {
			if (bitmapImage1.getHeight() > getFont().getHeight()) {
				intHeight = bitmapImage1.getHeight() + 8;
			} else {
				intHeight = getFont().getHeight() + 8;
			}
		}
		return intHeight;
	}
	
	public int getPreferredWidth() {
		int intWidth = 0;
		if (bitmapImage0.getWidth() > bitmapImage1.getWidth()) {
			intWidth = 4 + bitmapImage0.getWidth() + 4 + getFont().getAdvance(strLabel) + 4;
		} else {
			intWidth = 4 + bitmapImage1.getWidth() + 4 + getFont().getAdvance(strLabel) + 4;
		}
		return intWidth;
	}
	
	protected void layout(int width, int height) {
		setExtent(Math.min(width, getPreferredWidth()), Math.min(height, getPreferredHeight()));
	}

	protected void paint(Graphics graphics) {
		intUPosX = intUPosX + 0;
		intUPosY = intUPosY + 0;
		if (isFocus()) {
			// posisi image
			int imageX = 4;
			int imageY = 4;
			int imageWidth = bitmapImage1.getWidth();
			int imageHeight = bitmapImage1.getHeight();
			graphics.drawBitmap(imageX, imageY, imageWidth, imageHeight, bitmapImage1, 0, 0);
			
			// posisi text
//			graphics.setColor(focusedForegroundColor);
//			graphics.setGlobalAlpha(255);
//			int textX = bitmapImage.getWidth() + 8;
//			int textY = (getHeight() - getFont().getHeight()) / 2;
//			graphics.drawText(label, textX, textY);
		} else {
			// posisi image
			int imageX = 4;
			int imageY = 4;
			int imageWidth = bitmapImage0.getWidth();
			int imageHeight = bitmapImage0.getHeight();
			graphics.drawBitmap(imageX, imageY, imageWidth, imageHeight, bitmapImage0, 0, 0);
			
			// posisi text
//			graphics.setColor(focusedForegroundColor);
//			graphics.setGlobalAlpha(255);
//			int textX = bitmapImage.getWidth() + 8;
//			int textY = (getHeight() - getFont().getHeight()) / 2;
//			graphics.drawText(label, textX, textY);
		}
	}
	
	public boolean isFocusable() {
		return true;
	}
	
	protected void drawFocus(Graphics graphics, boolean on) {
	}
	
	protected void onFocus(int direction) {
		super.onFocus(direction);
		invalidate();
	}
	
	protected void onUnfocus() {
		super.onUnfocus();
		invalidate();
	}
	
	protected boolean navigationClick(int status, int time) {
		fieldChangeNotify(0);
		return true;
	}
	
	protected boolean keyChar(char character, int status, int time) {
		if (character == Keypad.KEY_ENTER) {
			fieldChangeNotify(0);
			return true;
		}
		return super.keyChar(character, status, time);
	}

}
